package com.xhui.vimarket.zkplus.zul.attr.mod.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Div;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Span;
import org.zkoss.zul.Textbox;

import com.xhui.vimarket.zkplus.ui.id.ZKRebuildUuid;
import com.xhui.vimarket.zkplus.utils.ZkUiUtil;
import com.xhui.vimarket.zkplus.zul.attr.AttrExt;
import com.xhui.vimarket.zkplus.zul.attr.bean.AttrInst;
import com.xhui.vimarket.zkplus.zul.attr.bean.AttrTmpl;
import com.xhui.vimarket.zkplus.zul.attr.bean.ValiVo;
import com.xhui.vimarket.zkplus.zul.attr.mod.AttrCompConstants;
import com.xhui.vimarket.zkplus.zul.attr.mod.AttrCompUtil;

/**
 * @版权：福富软件 版权所有 (c) 2010
 * @文件：com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrComboboxComp.java
 * @所含类：
 * @author: wuyx
 * @version: V1.0
 * @see:
 * @创建日期：2010-12-20
 * @功能说明：属性控件接口
 * @修改记录： =============================================================<br>
 *        日期:2010-12-20 创建
 *        =============================================================<br>
 */
public final class AttrComboboxComp extends AttrBaseComp {
    
    /**
     * 实例.
     */
    private static AttrComboboxComp inst = new AttrComboboxComp();
    
    /**
     * 构造函数. 
     */
    private AttrComboboxComp() {
    }
    
    /**
     * 获取实例.
     * 
     * @return AttrBaseComp
     */
    public static AttrComboboxComp instance() {
        return AttrComboboxComp.inst;
    }
    
    /**
     * 生成组件.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 组件模板
     * @param comp 显示容器组件
     */
    @Override
    public void create(final AttrExt attrExt, final AttrTmpl attrTmpl, final Component comp) {
        super.create(attrExt, attrTmpl, comp);
        // panel
        final Hbox hbox = new Hbox();
        hbox.setAttribute(AttrBaseComp.COMP_MODE, attrTmpl.getDisplayMode());
        hbox.setAttribute(AttrBaseComp.COMP_ATTRID, attrTmpl.getAttrId());
        hbox.setAttribute(AttrBaseComp.COMP_CANTNULL, AttrCompUtil.getStrCantNull(attrTmpl
            .isCantNull()));
        hbox.setAttribute(AttrBaseComp.COMP_COMP_NAME, attrTmpl.getName());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_LENGTH, attrTmpl.getValueLength());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_FROM, attrTmpl.getValueFrom());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_TO, attrTmpl.getValueTo());
        AttrCompUtil.setCompAttr(hbox, AttrBaseComp.COMP_VALUE_REX, attrTmpl.getValidExpr());
        hbox.setWidth("100%");
        hbox.setWidths("40%,60%");
        if (attrTmpl.getHint() != null) {
            hbox.setTooltiptext(attrTmpl.getHint());
        }
        final Div labDiv = new Div();
        final Label label = new Label(attrTmpl.getName());
        final Combobox combobox = new Combobox();
        //重构Uuid用于压力测试。
        ZKRebuildUuid.rebuildUuidById(combobox, 
            "cb_" + (attrTmpl.getAttrId() > 0 ? attrTmpl.getAttrId() : "_" 
                + Math.abs(attrTmpl.getAttrId())));
        // info
        final Span infDiv = this.createRulebar(attrExt, hbox, attrTmpl);
        // add to zul
        hbox.setParent(comp);
        labDiv.setParent(hbox);
        label.setParent(labDiv);
        infDiv.setParent(labDiv);
        combobox.setParent(hbox);
        
        super.afterCreate(attrExt, attrTmpl, comp);
    }
    
    /**
     * 设置值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param attrInstList 属性实例
     */
    @Override
    public void setValue(final AttrExt attrExt, final Component comp,
        final List<AttrInst> attrInstList,Map<String, Integer> attrViewMap) {
        super.setValue(attrExcom.xhui.vimarket.zkplusstList,attrViewMap);
        
    }

    com.xhui.vimarket.zkplusnheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.impl.AttrBaseComp#signRuleHint(com.xhui.vimarket.zkplus.zul.attr.AttrExt, org.zkoss.zk.ui.Component)
     * @author wuyx
     * 2012-2-16 wuyx
     */
    @Override
    public ArrayList<ValiVo> signRuleHint(AttrExt attrExt, Component comp) {
        super.signRuleHint(attrExt, comp);
        if (AttrCompUtil.isCantNull(comp)) { // 必填
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) {
                ValiVo retVo = new ValiVo();
                ArrayList<ValiVo> retVoList = new ArrayList<ValiVo>();
                retVoList.add(retVo);
                return retVoList;
            }
        }
        return null;
    }
    
    /**
     * 获取值.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getValue(final AttrExt attrExt, final Component comp) {
        super.getValue(attrExt, comp);
        return null;
    }
    
    /**
     * 取空值属性实例.
     * 
     * @param attrExt 组件控件
     * @param attrTmpl 模板
     * @return ArrayList<AttrInst>
     */
    @Override
    public ArrayList<AttrInst> getNullValue(final AttrExt attrExt, final AttrTmpl attrTmpl) {
        super.getNullValue(attrExt, attrTmpl);
        return null;
    }
    
    /**
     * 设置为不允许修改.
     * 
     * @param attrExt 组件控件
     * @param comp 组件
     * @param tflag 设置值，true/false
     */
    @Override
    public void setDisabled(AttrExt attrExt, Component comp, boolean tflag) {
        super.setDisabled(attrExt, comp, tflag);
        ZkUiUtil.setDisabled(comp, tflag);
    }
    
    @Override
    public List<RetVO> validate(AttrExt attrExt, Component comp) {
        if (AttrCompUtil.isCantNull(comp)) { // 需限制不可空
            ArrayList<AttrInst> valList = this.getValue(attrExt, comp);
            if (valList == null || valList.size() < 1) { // 没有值
                return AttrCompUtil.getRetVoList(AttrCompConstants.RET_ERROR, AttrCompUtil
                    .getCompName(comp)
                    + AttrCompConstants.STRING_CANT_NULL, comp);
            }
            return null; // 通过
        }
        return null; // 通过
    }
    
    /**
     * getTextbox.
     * 
     * @param root Component
     * @return Textbox
     */
    public static Combobox getCombobox(Component root) {
        // 取输入框
        // hbox // Combobox(1) 
        returncom.xhui.vimarket.zkplust.getFirstChild().getNextSibling();
    }
    @Override
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setAttrView(org.zkoss.zk.ui.Component, Map<String, Integer>)
     * @author chenmh
     * 2011-6-16 chenmh
     */
    public void setAttrView(final Component comp, Map<String, Integer> attrViewMap) {
        if (attrViewMap != null && attrViewMap.size() > 0) {
            String attrJC = comp.getAttribute(AttrBaseComp.COMP_JAVACODE) + "";     
            if (!StrUtil.isNullOrEmpty(attrJC)) {
                Integer viewType = attrViewMap.get(attrJC);
                // 取输入框
                final Combobox combobox = getCombobox(comp);
                if (combobox != null) {
                    combobox.setVisible(false);
                    if (AttrBaseComp.VIEW_TYPE_SHOW == viewType) {
                        combobox.setDisabled(true);
                        combobox.setVisible(true);
                    } else if (AttrBaseComp.VIEW_TYPE_EDIT == viewType) {
                        combobox.setVisible(true);
                    }
                }
            }
        }
    }
    /**
     * 设置信息是否可见.
     * 
com.xhui.vimarket.zkplusomp 组件
     * @param attrViewMap 信息是否可见映照
     */
    @Override
    /**
     * {@inheritDoc}
     * @see com.xhui.vimarket.zkplus.zul.attr.mod.AttrComp
     * #setAttrView(org.zkoss.zk.ui.Component, Map<String, Integer>)
     * @author chenmh
     * 2011-6-16 chenmh
     */

    public void setAttrConstrain(final Component comp, Map<Object, String> attrConstrainMap){
        // TODO Auto-generated method stub
        if (attrConstrainMap != null && attrConstrainMap.size() > 0) {
            String attrJC = comp.getAttribute(AttrBaseComp.COMP_JAVACODE) + "";
            String val = attrConstrainMap.get(attrJC);
            if (val != null && val.indexOf(AttrBaseComp.CNS_TYPE_EXTRA_NCP + ";") != -1) {
                final Textbox textbox = getCombobox(comp);
                ZkUiUtil.setCompDisableCopy(textbox, "对不起，您没有拷贝的权限！",
                    ZkUiUtil.DISPLAY_COPY_CONTEXT_MOLD_MENU);
                
            }
        }
    }
    
}
